<?php
session_start();
header("Content-type:text/html;charset=utf-8");
$link = mysqli_connect('localhost','root','123456');
mysqli_select_db($link,'test');
if (!$link) {
    die("连接失败:".mysqli_connect_error());
}
function db_connect() {
    $result = new mysqli('localhost', 'root', '123456', 'test');
    if (!$result) {
        throw new Exception('不能连接到数据库！');
    } else {
        return $result;
    }
}

$Username = $_POST['Username'];
$pwd = $_POST['Password'];
$rpwd = $_POST['Re_Password'];
$code = $_POST['Code'];

if(strlen($pwd) < 5||strlen($pwd)>10){

    echo"<script>alert('你的密码需要5~10位');window.location.href='register2.php'</script>";
    exit;
}
else if($rpwd != $pwd){

    echo"<script>alert('你输入的两次密码不一致,请重新输入');window.location.href='register2.php'</script>";
    exit;
} else if($code != $_SESSION['authcode']) {
    echo "<script>alert('验证码错误！重新填写');window.location.href='register2.php'</script>";
    //判断验证码是否填写正确
} else{
    $sql= "insert into user (name,password)values('$Username','$pwd')";

}

if(!(mysqli_query($link,$sql))){
    echo "<script>alert('注册失败');window.location.href='register2.php'</script>".$mysqli_stmt->error;
    exit;
}else{
    echo "<script>alert('注册成功！去登陆');window.location.href='Sign_in1.html'</script>";
}

?>

